﻿using IBLL.SH_ADF0979IBLL;
using IDAL.SH_ADF0979IDAL;
using Microsoft.EntityFrameworkCore;
using MysqlforDataWatch;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tools.MyConfig;
using static Tools.FileOperation.CSVFileImport;

namespace BLL.SH_ADF0979BLL
{
    public class AnalysisData_All_BLL : BaseBLL<StatisticAllHalfHour>, IAnalysisData_All_IBLL
    {
        private readonly IAnalysisData_All_IDAL _AnalysisData_All_DAL;
        //private readonly DbContext _DB;
        public AnalysisData_All_BLL(IAnalysisData_All_IDAL AnalysisData_All_DAL)
        {
            this._AnalysisData_All_DAL = AnalysisData_All_DAL;
            base.CurrentDal = AnalysisData_All_DAL;//构造函数把实例传入给basebll里的currentdal

            
            //_DB = DB;
        }
        public override void SetCurrentDal()
        {
            //base.CurrentDal = this._AnalysisData_All_DAL;//这样好像没用，无法获取实例给CurrentDal
        }


        public string JudgeandMergeStatisticAllHalfHourData(FileInfo file, CSVStatisticDataAllImport importstruct, ref List<StatisticAllHalfHour> blist, string vehicleid, in VehicleIDPara vehicleIDPara)
        {
            return _AnalysisData_All_DAL.JudgeandMergeStatisticAllHalfHourData(file,importstruct, ref blist, vehicleid, in vehicleIDPara);
        }
        /// <summary>
        /// 根据日期范围加载加速度和位移统计数据到前端，从统一的表里拿数据
        /// </summary>
        /// <param name="sd"></param>
        /// <param name="ed"></param>
        /// <param name="vehicleid"></param>
        /// <returns></returns>
        public async Task<IQueryable> LoadAccandDisDatafromStatisticAll(DateTime sd, DateTime ed, string vehicleid)
        {
            var list = await Task.Run(() => CurrentDal.LoadEntities(a => a.datadate >= sd && a.datadate <= ed && a.vehicle == vehicleid && (a.chantitle.Contains("acc") || a.chantitle.Contains("dis")), vehicleid).Select(a => new
            {
                a.chantitle,
                a.max,
                a.min,
                a.filename
            }));
            var t=list.ToList();
            var maxminlist = list.GroupBy(x =>
                 x.chantitle).Select(x => new
                 {
                     chantitle = x.Key,
                     max = x.Max(a => a.max),
                     min = x.Min(a => a.min),
                     filenamemax= list.Where(s=>s.max== x.Max(a => a.max)).FirstOrDefault().filename,
                     filenamemin = list.Where(s => s.min == x.Min(a => a.min)).FirstOrDefault().filename
                 }).OrderBy(b => b.chantitle).ToList();
                     
            return maxminlist.AsQueryable();
        }

        /// <summary>
        /// 这里已经检索了轮心力的数据，包括轮心加速度和轮心力
        /// </summary>
        /// <param name="sd"></param>
        /// <param name="ed"></param>
        /// <param name="vehicleid"></param>
        /// <returns></returns>
        public async Task<IQueryable> LoadWFTDamage(DateTime sd, DateTime ed, string vehicleid)
        {
            var damagelist = await Task.Run(() => CurrentDal.LoadEntities(a => a.datadate >= sd && a.datadate <= ed
     && a.vehicle == vehicleid, vehicleid).Select(a => new
     {
         a.chantitle,
         a.damagek3,
         a.damagek5,
         a.max,
         a.min,
         a.filename
     }));
            var maxmindamlist = damagelist.GroupBy(x =>
                x.chantitle).Select(x => new
                {
                    chantitle = x.Key,
                    damagek5 = x.Sum(a => a.damagek5),
                    damagek3 = x.Sum(a => a.damagek3),
                    max = x.Max(a => a.max),
                    min = x.Min(a => a.min),
                    filenamemax = damagelist.Where(s => s.max == x.Max(a => a.max)).FirstOrDefault().filename,
                    filenamemin = damagelist.Where(s => s.min == x.Min(a => a.min)).FirstOrDefault().filename
                }).Where(a => a.chantitle.Contains("wftf") || a.chantitle.Contains("accxwhl") ||a.chantitle.Contains("accywhl") || a.chantitle.Contains("acczwhl")).ToList().OrderBy(b => b.chantitle);
            return maxmindamlist.AsQueryable();
        }

        public async Task<IQueryable> LoadWFTDamageCumulation(DateTime sd, DateTime ed, string vehicleid)
        {
            //这里考虑按每一天进行求和损伤
            var damagelist = await Task.Run(() => _AnalysisData_All_DAL.LoadEntities(a => a.datadate >= sd && a.datadate <= ed.AddDays(1) && a.vehicle == vehicleid, vehicleid).GroupBy(x => new
            {
                x.chantitle,
                x.datadate.Value.Year,
                x.datadate.Value.Month,
                x.datadate.Value.Day
            }
            ).Select(x => new
            {
                x.Key.chantitle,
                datetime = x.Key.Year.ToString() + "-" + x.Key.Month.ToString() + "-" + x.Key.Day.ToString(),
                damagek5 = Math.Round((double)x.Sum(a => a.damagek5), 0),
                damagek3 = Math.Round((double)x.Sum(a => a.damagek3), 0),
            }).Where(a => a.chantitle.Contains("wftf") || a.chantitle.Contains("accxwhl") || a.chantitle.Contains("accywhl") || a.chantitle.Contains("acczwhl")).ToList().OrderBy(b => b.chantitle).ThenBy(b => b.datetime));
            return damagelist.AsQueryable();
        }

    }
}
